//
//  RHVisitorView.swift
//  微博新浪
//
//  Created by hezerong on 16/6/6.
//  Copyright © 2016年 Rocky. All rights reserved.
//

import UIKit
import SnapKit


protocol visitdelegate{

   func didLogin()
}



class RHVisitorView: UIView {
    
    //代理属性
    var delegate:visitdelegate?
    
    //添加子控件，从上往下，从左往右
    
    //图标
    lazy var iconView:UIImageView = UIImageView(imageName: "visitordiscover_feed_image_house")
    
    //转圈图片
    lazy var circleView:UIImageView = UIImageView(imageName: "visitordiscover_feed_image_smallicon")
    
    //遮罩图片
    lazy var maskIconView:UIImageView = UIImageView(imageName: "visitordiscover_feed_mask_smallicon")
    
    //文字lable
    lazy var textLable:UILabel = UILabel(text: "登录后，别人评论你的微博，给你发送消息，你都会收到！",  textAlignment: .Left)
    
    //注册按钮
    lazy var registButton:UIButton = UIButton(titlt: "注册",
                                                titleColor: UIColor.orangeColor(),
                                                font: 14,
                                                backGroundImage: "common_button_white_disable",
                                                target: self, action: "register:")
    
    //登录按钮
    lazy var loginButton:UIButton = UIButton(titlt: "登录",
                                              titleColor: UIColor.darkGrayColor(),
                                              font: 14,
                                              backGroundImage: "common_button_white_disable",
                                              target: self, action: "login:")

  
    //访客视图的构造函数
    init() {
        
        super.init(frame:(CGRect(x: 0, y: 64, width: UIScreen.mainScreen().bounds.size.width, height: UIScreen.mainScreen().bounds.size.height - 64 - 44)))
        
        setUpUI()
    }

    //required,如果说父类定义一个函数，使用了required修饰，则子类必须重写该方法
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

extension RHVisitorView{
    
    func setVisitView(title:String,icon:String,isHome:Bool=false){
    
    textLable.text = title
        
    iconView.image = UIImage(named: icon)
        
        if isHome{
        
           circleView.hidden = false
            maskIconView.hidden = false
        }
    
    }

    
    /**
     为circleview添加转圈动画
     */
    func addAnimation(){
    
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        
        //动画持续的时间
        animation.duration = 15
        
        //旋转的弧度
        animation.toValue = 2 * M_PI
        
        //无限旋转
        animation.repeatCount = MAXFLOAT
        
        //执行完之后移除动画
        animation.removedOnCompletion = false
        
        //把动画添加到view中
        self.circleView.layer.addAnimation(animation, forKey: nil)
        
    }
     
    //自动布局，添加子控件， 给子控件设置初始值
    func setUpUI(){
    
    backgroundColor = UIColor(white: 237/255, alpha: 1)
        
        
        circleView.hidden = true
        maskIconView.hidden = true
        
    //添加子控件
        addSubview(iconView)
        addSubview(circleView)
        addSubview(maskIconView)
        addSubview(textLable)
        addSubview(registButton)
        addSubview(loginButton)
        
    //给子控件添加默认值
//        iconView.image = UIImage(named: "visitordiscover_feed_image_house")
//        circleView.image = UIImage(named: "visitordiscover_feed_image_smallicon")
//        maskIconView.image = UIImage(named: "visitordiscover_feed_mask_smallicon")
//        textLable.text = "登录后，别人评论你的微博，给你发送消息，你都会收到！"
//        registButton.setTitle("注册", forState: .Normal)
//        loginButton.setTitle("登录", forState: .Normal)
//        
    
        //控件自动布局
        iconView.snp_makeConstraints { (make) -> Void in
           make.centerX.equalTo(self)
           make.centerY.equalTo(self).offset(-100)
   
        }
        
        self.bringSubviewToFront(iconView)
        
        
        circleView.snp_makeConstraints { (make) -> Void in
            
            make.center.equalTo(iconView)
        }
        
        
        maskIconView.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(circleView)
            make.centerY.equalTo(circleView).offset(-10)
            make.width.equalTo(self)

        }
        
        textLable.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(circleView)
            make.centerY.equalTo(circleView.snp_bottom).offset(10)
            make.left.equalTo(self).offset(80)
            make.right.equalTo(self).offset(-80)
            
        }
        
        registButton.snp_makeConstraints { (make) -> Void in
            
            make.centerY.equalTo(textLable.snp_bottom).offset(50)
            make.left.equalTo(self).offset(50)
            make.width.equalTo(100)
            
        }
        
        loginButton.snp_makeConstraints { (make) -> Void in
            
            make.centerY.equalTo(textLable.snp_bottom).offset(50)
            make.right.equalTo(-50)
            make.width.equalTo(100)
        }
        
        
        //添加动画
        addAnimation()
    }
}

//登录注册方法
extension RHVisitorView{
 
    func register(button:UIButton){
    
        print("注册")
    }
    
    func login(button:UIButton){
    
       print("登录")
        
        delegate?.didLogin()
    }

}